<template>
  <div class="studentList">
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
      <el-form-item label="姓名">
        <el-input v-model="formInline.name" placeholder="请输入姓名查询"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="find">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="compData" border style="width: 100%">
      <el-table-column prop="id" label="序号" align="center"></el-table-column>
      <el-table-column prop="name" label="姓名" align="center"></el-table-column>
      <el-table-column prop="age" label="年龄" align="center"></el-table-column>
      <el-table-column prop="sex_text" label="性别" align="center"></el-table-column>
      <el-table-column prop="number" label="学号" align="center"></el-table-column>
      <el-table-column prop="whichClass" label="班级" align="center"></el-table-column>
      <el-table-column prop="state_text" label="状态" align="center"></el-table-column>
      <el-table-column prop="address" label="地址" align="center"></el-table-column>
      <el-table-column prop="phone" label="电话" align="center"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, next, pager, jumper"
        :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import {deleteStu, getStu} from "@/router/svc/stu";
import {Message} from "element-ui";

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1, //当前页数
      pageSize: 10, // 每页行数
      total: 0,
      formInline: {
        name: ''
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData(params) {
      getStu(params).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data
          this.tableData.forEach(item => {
            item.sex_text = item.sex === "1" ? '男' : '女'
            item.state_text = item.state === "1" ? '已入学'
                : (item.state === "12" ? '未入学' : '已毕业')
          })
          this.total = res.data.data.length
          this.pageSize = 10
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
    },
    handleCurrentChange(val) {
      this.currentPage = val
    },
    del(row) {
      deleteStu(row.id).then(res => {
        if (res.data.code === 200) {
          Message({message: '成功删除 ' + res.data.data, type: 'success'})
          this.getData()
        }
      })
    },
    find() {
      this.getData(this.formInline)
    },
    reset() {
      this.formInline = {name: ''}
      this.getData()
    }
  },
  computed: {
    compData() {
      return this.tableData.slice((this.currentPage - 1) * this.pageSize,
          this.currentPage * this.pageSize)
    }
  }
}
</script>

<style lang="scss">
.studentList {
  .demo-form-inline {
    text-align: left;
  }

  .el-pagination {
    text-align: left;
    margin-top: 20px;
  }
}
</style>